<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
class LinkController extends CommonController {
    //列表
    public function index(){
      $nav_object = D('link');
      $map['status'] = array('egt', '0'); // 禁用和正常状态
      $Page = new Page($nav_object->where($map)->count(),C('ADMIN_CONFIG.page_size'));
      $Page->setConfig('prev','«');
      $Page->setConfig('next','»');
      $data_list     = $nav_object
          ->where($map)
          ->order('id asc')
          ->limit($Page->firstRow.','.$Page->listRows)
          ->select();
      $this->assign('mark','system');
      $this->assign('umark','link');
      A('list')->setMetaTitle('友情链接列表')
          ->setBreadCrumb(array('系统','系统功能','友情链接'))
          ->addTopButton('add',array('title'=>'链接添加'))
          ->addTopButton('resume')
          ->addTopButton('delete')
          ->addTableColumn('id','ID','number',50)
          ->addTableColumn('img','链接图片','image',100,'false')
          ->addTableColumn('title','标题')
          ->addTableColumn('url','链接地址')
          ->addTableColumn('sort','排序','sort',80)
          ->addTableColumn('status','状态','status',60)
          ->addTableColumn('ctime','创建时间','date',120)
          ->addTableColumn('','操作','rightbtn',200,'false')
          ->setTableDataList($data_list)
          ->setTableDataPage($Page->show())
          ->addRightButton('edit')
          ->addRightButton('forbid')
          ->addRightButton('delete')
          ->fetch();
    }
    //添加
    public function add(){
      $link_object = D('link');
      if (IS_POST) {
          $data = $link_object->create();
          if ($data) {
              $id = $link_object->add($data);
              if ($id) {
                  $ajax = array(
                    'status'=>1,
                    'text'=>'链接新增成功',
                    'url'=>U('index')
                  );
              } else {
                  $ajax = array(
                    'status'=>0,
                    'text'=>'链接新增失败',
                    'url'=>''
                  );
              }
          }else {
              $ajax = array(
                'status'=>0,
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
      }else {
        $this->assign('mark','system');
        $this->assign('umark','link');
        A('form')->setMetaTitle('友情链接添加')
        ->setBreadCrumb(array('系统','系统功能','友情链接','友情链接添加'))
        ->setPostUrl(U('add'))
        ->addFormItem('title','text','链接标题','',array(),array('required'=>true))
        ->addFormItem('img','image','链接图片')
        ->addFormItem('url','text','链接地址','链接地址',array(),array('required'=>true))
        ->addFormItem('sort','text','排序','排序')
        ->addFormItem('status','switch','状态','',array('启用','禁用'))
        ->setFormData(array('status'=>1,'sort'=>0))
        ->fetch();
      }
    }
    //编辑
    public function edit(){
      $link_object = D('link');
      if (IS_POST) {
          $data = $link_object->create();
          if ($data) {
              $rs = $link_object->save($data);
              if ($rs) {
                  $ajax = array(
                    'status'=>1,
                    'text'=>'链接编辑成功',
                    'url'=>U('index')
                  );
              } else {
                  $ajax = array(
                    'status'=>0,
                    'text'=>'链接编辑失败',
                    'url'=>''
                  );
              }
          }else {
              $ajax = array(
                'status'=>0,
                'text'=>$link_object->getError(),
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
      }else {
        $this->assign('mark','system');
        $this->assign('umark','nav');

        $id = I('get.ids',0,'intval');
        $form_data = $link_object->find($id);


        A('form')->setMetaTitle('链接编辑')
        ->setBreadCrumb(array('系统','系统功能','友情链接','链接编辑'))
        ->setPostUrl(U('edit'))
        ->addFormItem('id','hidden')
        ->addFormItem('title','text','链接标题','',array(),array('required'=>true))
        ->addFormItem('img','image','链接图片')
        ->addFormItem('url','text','链接地址','链接地址',array(),array('required'=>true))
        ->addFormItem('sort','text','排序','排序')
        ->addFormItem('status','switch','状态','',array('启用','禁用'))
        ->setFormData($form_data)
        ->fetch();
      }
    }
    //状态修改
    public function setStatus(){
        if (IS_GET) {
          $ids = I('get.ids');
          $status = I('get.status');
          $link_object = M('link');
          $map = array(
            'id' => array('in',$ids),
          );
          $save = array(
            'status' => $status,
          );
          $rs = $link_object->where($map)->save($save);
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'链接状态修改成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'链接状态修改失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
    //删除
    public function delete(){
        if (IS_GET) {
          $ids = I('get.ids');
          $link_object = M('link');
          $map = array(
            'id' => array('in',$ids),
          );
          $rs = $link_object->where($map)->delete();
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'链接删除成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'链接删除失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
}
